<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare (strict_types=1);

namespace app\license\controller;

use app\base\BaseController;
use app\License\model\License as LicencsList;
use app\license\model\License;
use app\license\model\LicenseCert;
use app\License\validate\LicenseCheck;
use Psr\Log\NullLogger;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{

    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $whereOr = array();
            if (!empty($param['keywords'])) {
                $where[] = ['a.legal_name|a.company_name', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['cert_project_name'])) {
                $where[] = ['b.cert_project_name', 'like', '%' . $param['cert_project_name'] . '%'];
                $where[] = ['b.status', '=', 4];
            }
            if (!empty($param['safety_valid_date']) && !empty($param['safety_valid_date1'])) {
                $where[] = ['a.safety_valid_date', '>=', $param['safety_valid_date']];
                $where[] = ['a.safety_valid_date', '<=', $param['safety_valid_date1']];
            }
            if (!empty($param['c_type'])) {
                $where[] = ['a.c_type', '=', $param['c_type']];
            }
            if (!empty($param['province'])) {
                $where[] = ['a.province', '=', $param['province']];
            }
            if (!empty($param['city'])) {
                $where[] = ['a.city', '=', $param['city']];
            }
            if (!empty($param['area'])) {
                $where[] = ['a.area', '=', $param['area']];
            }
            if (!empty($param['cert_cates'])) {

                $where[] = ['c.cert_serial|c.cert_from|c.cert_level', 'like', '%' . $param['cert_cates'] . '%'];
            }
            if (!empty($param['cert_from_type'])) {
                $where[] = ['c.cert_from_type', '=', $param['cert_from_type']];
            }

            $where[] = ['a.delete_time', '=', 0];

            $where[] = ['a.license_type', '=', 1];
            $model = new License();
            $list = $model->get_list($param, $where, $whereOr);
            return table_assign(0, '', $list);
        } else {

            return view();
        }
    }

    public function my()
    {

        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $whereOr = array();
            if (!empty($param['keywords'])) {
                $where[] = ['a.legal_name|a.company_name', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['cert_project_name'])) {
                $where[] = ['b.cert_project_name', 'like', '%' . $param['cert_project_name'] . '%'];
                $where[] = ['b.status', '=', 4];
            }
            if (!empty($param['safety_valid_date']) && !empty($param['safety_valid_date1'])) {
                $where[] = ['a.safety_valid_date', '>=', $param['safety_valid_date']];
                $where[] = ['a.safety_valid_date', '<=', $param['safety_valid_date1']];
            }
            if (!empty($param['c_type'])) {
                $where[] = ['a.c_type', '=', $param['c_type']];
            }
            if (!empty($param['province'])) {
                $where[] = ['a.province', '=', $param['province']];
            }
            if (!empty($param['city'])) {
                $where[] = ['a.city', '=', $param['city']];
            }
            if (!empty($param['area'])) {
                $where[] = ['a.area', '=', $param['area']];
            }
            if (!empty($param['cert_cates'])) {

                $where[] = ['c.cert_serial|c.cert_from|c.cert_level', 'like', '%' . $param['cert_cates'] . '%'];
            }
            if (!empty($param['cert_from_type'])) {
                $where[] = ['c.cert_from_type', '=', $param['cert_from_type']];
            }

            $where[] = ['a.delete_time', '=', 0];

            $where[] = ['a.admin_id', '=', $this->uid];
            $model = new License();
            $list = $model->get_list($param, $where, $whereOr);
            return table_assign(0, '', $list);
        } else {
            return view();
        }

    }

    public function sell()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $whereOr = array();
            if (!empty($param['keywords'])) {
                $where[] = ['a.legal_name|a.company_name', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['cert_project_name'])) {
                $where[] = ['b.cert_project_name', 'like', '%' . $param['cert_project_name'] . '%'];
                $where[] = ['b.status', '=', 4];
            }
            if (!empty($param['safety_valid_date']) && !empty($param['safety_valid_date1'])) {
                $where[] = ['a.safety_valid_date', '>=', $param['safety_valid_date']];
                $where[] = ['a.safety_valid_date', '<=', $param['safety_valid_date1']];
            }
            if (!empty($param['c_type'])) {
                $where[] = ['a.c_type', '=', $param['c_type']];
            }
            if (!empty($param['province'])) {
                $where[] = ['a.province', '=', $param['province']];
            }
            if (!empty($param['city'])) {
                $where[] = ['a.city', '=', $param['city']];
            }
            if (!empty($param['area'])) {
                $where[] = ['a.area', '=', $param['area']];
            }
            if (!empty($param['cert_cates'])) {

                $where[] = ['c.cert_serial|c.cert_from|c.cert_level', 'like', '%' . $param['cert_cates'] . '%'];
            }
            if (!empty($param['cert_from_type'])) {
                $where[] = ['c.cert_from_type', '=', $param['cert_from_type']];
            }

            $where[] = ['a.delete_time', '=', 0];
            $where[] = ['a.c_type', '=', 1];
            $where[] = ['a.license_type', '=', 1];
            $model = new License();
            $list = $model->sell_get_list($param, $where, $whereOr);
            return table_assign(0, '', $list);
        } else {

            return view();
        }
    }


    //添加&&编辑
    public function sign()
    {
        $param = get_params();
        $model = new License();
        $res = $model->field(true)->update($param);
        if ($res) {
            return to_assign(0, "标记成功");
        } else {
            return to_assign(1, '标记失败');
        }
    }

    //添加&&编辑
    public function add()
    {
        $param = get_params();
        unset($param['file']);
        unset($param['scene']);
        if (request()->isAjax()) {

            if (!empty($param['id']) && $param['id'] > 0) {
                $param['update_time'] = time();
                $old = Db::name('license')->where(['id' => $param['id']])->find();
                $model = new License();
                if (!empty($model->where('company_name', $param['company_name'])->where('id', '<>', $param['id'])->where('delete_time', 0)->find())) {
                    return to_assign(1, '该公司信息已存在，请勿重复录入');
                }
                $res = $model->field(true)->update($param);
                if ($res) {
                    add_log('edit', $param['id'], $param);
                    to_log($this->uid, $param, $old);
                    return to_assign(0, "修改成功");
                } else {
                    return to_assign(1, '操作失败');
                }
            } else {
                $param['create_time'] = time();
                $param['admin_id'] = $this->uid;
                $model = new License();
                if (!empty($model->where('company_name', $param['company_name'])->where('delete_time', 0)->find())) {
                    return to_assign(1, '该公司信息已存在，请勿重复录入');
                }
                $aid = $model->field(true)->insertGetId($param);
                if ($aid) {

                    add_log('add', $aid, $param);
                    return to_assign(0, "添加成功");
                } else {
                    return to_assign(1, '操作失败');
                }
            }
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            View::assign('id', $id);
            if ($id > 0) {
                $model = new License();
                $detail = $model->detail($id);
                View::assign('detail', $detail);
            }
            return view();
        }
    }

    //删除
    public function delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            $data['id'] = $id;
            $data['delete_time'] = time();
            if (Db::name('license')->update($data) !== false) {
                add_log('delete', $id);

                return to_assign(0, "删除成功");
            } else {
                return to_assign(1, "删除失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }


    public function archive()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $id = isset($param['id']) ? $param['id'] : 0;

            $where = array();
            $where[] = ['a.delete_time', '=', 0];
            $where[] = ['a.license_id', '=', $id];
            $where[] = ['a.status', '=', 4];
            $whereOr = [];

            $model = new LicenseCert();
            $list = $model->get_list($param, $where, $whereOr);

            return table_assign(0, '', $list);
        } else {
            $param = get_params();
            $id = isset($param['id']) ? $param['id'] : 0;
            View::assign('id', $id);
            return view();
        }

    }

    public function archive_add()
    {

        $param = get_params();
        if (request()->isAjax()) {
            $check_crets = $param['check_crets'] ?? [];
            if (empty($check_crets)) {
                return to_assign(1, '请先保存资质类型');
            }
            $param['sign_state'] = 1;
            unset($param['check_crets']);
            if (!empty($param['id']) && $param['id'] > 0) {
                $param['update_time'] = time();
                $old = Db::name('license_cert')->where(['id' => $param['id']])->find();
                $model = new LicenseCert();
                $res = $model->field(true)->update($param);
                if ($res) {
                    Db::name('license_cert_cates')->where('cert_id', $param['id'])->delete();
                    if (!empty($check_crets)) {
                        $certs_all = [];
                        foreach ($check_crets as $k => $v) {
                            $certs = [];
                            list($a, $b, $c, $d) = explode('|', $v);
                            $certs['cert_serial'] = $a;
                            $certs['cert_from'] = $b;
                            $certs['cert_level'] = $c;
                            $certs['cert_from_type'] = $d;
                            $certs['cert_id'] = $param['id'];
                            $certs_all[] = $certs;
                        }
                        Db::name('license_cert_cates')->insertAll($certs_all);
                    }

                    add_log('edit', $param['id'], $param);
                    to_log($this->uid, $param, $old);
                    return to_assign(0, "修改成功");
                } else {
                    return to_assign(1, '操作失败');
                }
            } else {
                $param['status'] = 4;
                $aid = Db::name('license_cert')->insertGetId($param);
                if ($aid) {
                    if (!empty($check_crets)) {
                        $certs_all = [];
                        foreach ($check_crets as $k => $v) {
                            $certs = [];
                            list($a, $b, $c, $d) = explode('|', $v);
                            $certs['cert_serial'] = $a;
                            $certs['cert_from'] = $b;
                            $certs['cert_level'] = $c;
                            $certs['cert_from_type'] = $d;
                            $certs['cert_id'] = $aid;
                            $certs_all[] = $certs;
                        }
                        Db::name('license_cert_cates')->insertAll($certs_all);
                    }
                    return to_assign();
                } else {
                    return to_assign(1, '操作失败');
                }
            }
        } else {
            $id = $param['id'] ?? 0;
            View::assign('id', $id);
            if ($id > 0) {
                $model = new LicenseCert();
                $detail = $model->detail($id);
                $detail->cert_cates = Db::name('license_cert_cates')->where('cert_id', $id)->select();
                View::assign('detail', $detail);
            }
            $license_id = $param['license_id'] ?? 0;
            View::assign('license_id', $license_id);
            return view();
        }
    }

    //删除
    public function view()
    {
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        $model = new License();
        $detail = $model->detail($id);
        $detail['transact'] = Db::name('transact')->field('company_type,handle_person,deduct')->where('busine_type', 0)->where('delete_time', 0)->where('company_id', $detail->id)->select();
        $detail['license_cert'] = Db::name('license_cert')->field('report_name,deduct,is_zx,report_date')->where('delete_time', 0)->where('license_id', $detail->id)->select();
        $detail['bitjob_enterprise'] = Db::name('bitjob_enterprise')->field('battle_user_name,battle_money,battle_use')->where('delete_time', 0)->where('license_id', $detail->id)->select();
        $detail['self_sell'] = Db::name('self_sell')->field('sign_name,sign_money,deduct,cert_project_name')->where('delete_time', 0)->where('license_id', $detail->id)->select();
        View::assign('id', $id);
        View::assign('detail', $detail);
        return view();
    }

    //删除
    public function archive_delete()
    {
        if (request()->isDelete()) {
            $id = get_params("id");
            $data['id'] = $id;
            $data['delete_time'] = time();
            if (Db::name('license_cert')->update($data) !== false) {
                add_log('delete', $id);

                return to_assign(0, "删除成功");
            } else {
                return to_assign(1, "删除失败");
            }
        } else {
            return to_assign(1, "错误的请求");
        }
    }

    public function shou()
    {
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        $data['id'] = $id;
        $data['check_status'] = 1;
        if (Db::name('license')->update($data) !== false) {
            return to_assign(0, "授权成功");
        } else {
            return to_assign(1, "授权失败");
        }
    }


}
